

package org.smallbun.framework.toolkit;

import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 *工具类
 * @author SanLi
 * Created by 2689170096@qq.com on 2019/3/4 20:23
 */
public class ExcelUtil {
	public static void export(String fileName, Workbook workbook, HttpServletResponse response) {
		// 设置excel的文件名称
		String excelName = fileName + System.currentTimeMillis();
		// 重置响应对象
		response.reset();
		// 指定下载的文件名--设置响应头
		try {
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String(excelName.getBytes("gb2312"), StandardCharsets.ISO_8859_1)
							+ ".xls");
			// 设置响应头
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
